package com.sqsf.web;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "StudentServlet", value = "/student")
public class StudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = request.getServletContext();
        //使用spring-web的jar包中提供的工具类WebApplicationContextUtils
        //调用其静态方法getWebApplicationContext 从servletContext域对象中获取spring容器对象
        //因为 是使用的spring-web提供的监听器 将spring容器存储到servletContext域对中 我们也不知道name
        WebApplicationContext spring = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        //从spring容器对象中获取指定对象
        Student bean = spring.getBean(Student.class);
        System.out.println(bean);
    }
}